home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-30 | 13.5 KB | 519 lines | [TEXT/CWIE] |
- /*
- File: TInternetAssistantDialog.cp
-
- Contains: Dialog implementing the Internet Setup Assistant
-
- Written by: Arno Gourdol
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TInternetAssistantInterview.h"
- #include "assert.h"
- #include "TApplication.h"
-
- #include <Resources.h>
- #include "ICTypes.h"
- #include "ICKeys.h"
- #include "ICAPI.h"
-
- void GetHostName(ConstStr255Param address, Str255 host);
-
-
-
- // --------------------------------------------------------------------
- // GetHostName
- // --------------------------------------------------------------------
- // Extract the host from a "user@host" string
-
- void GetHostName(ConstStr255Param address, Str255 host)
- {
- UInt16 atIndex = 0; // Index of the "@" character in the string
-
- host[0] = 0;
-
- for (UInt16 i = 1; i <= address[0]; i++)
- {
- if (address[i] == '@')
- {
- atIndex = i;
- break;
- }
- }
-
- if (atIndex > 0 && atIndex <= address[0])
- {
- host[0] = address[0] - atIndex;
- BlockMoveData(&address[atIndex + 1], &host[1], host[0]);
- }
- }
-
-
-
-
- // --------------------------------------------------------------------
- // TInternetAssistantDialog
- // --------------------------------------------------------------------
-
- TInternetAssistantDialog::TInternetAssistantDialog(TApplication* application) :
- TAssistant(kIntroPane, kNNTPHostPane, application),
- fInternetConfig('ICEx'),
- fMap(302, 303, 300, 301, 300)
- {
- // Get the default values from ICS and other sources
- // and store them in the parameters (<email>, <organization>...)
- GetDefaultValues();
- }
-
-
-
- // --------------------------------------------------------------------
- // ~TInternetAssistantDialog
- // --------------------------------------------------------------------
-
- TInternetAssistantDialog::~TInternetAssistantDialog()
- {
- }
-
-
-
- // --------------------------------------------------------------------
- // DrawUserItem
- // --------------------------------------------------------------------
- // Hook function called to draw custom user items
- // We use it to draw the custom map
-
- void TInternetAssistantDialog::DrawUserItem(TDrawContext& drawContext,
- short item,
- const CRect& frame)
- {
- TAssistant::DrawUserItem(drawContext, item, frame);
-
- if (GetDialogPane() == kLocationPane && item == kMapUserItem)
- {
- fMap.Draw(drawContext, frame);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // FilterEvent
- // --------------------------------------------------------------------
- // Hook function called to allow us to filter events
- // We use it to handle the map tracking on mouseDown
-
- Boolean TInternetAssistantDialog::FilterEvent(const EventRecord& event)
- {
- Boolean result = false;
-
- if (event.what == mouseDown && GetDialogPane() == kLocationPane)
- {
- // If a mousedown happened in the location pane...
- CPoint where(event.where);
- TDrawContext drawContext(GetGrafPtr());
- if (drawContext.Lock())
- {
- drawContext.ConvertFromScreen(where);
- // ... and it was the map user item...
- if (GetItemRect(kMapUserItem).Contains(where))
- {
- // ... pass the mouseDown to the map
- // We have to do this because only mouse-ups (thru ItemHit)
- // are passed usually to user items.
- fMap.MouseDown(GetItemRect(kMapUserItem));
- result = true;
- }
- drawContext.Unlock();
- }
- }
-
- if (!result)
- {
- result = TAssistant::FilterEvent(event);
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // FilterKey
- // --------------------------------------------------------------------
- // Hook function called when a key is pressed.
- // Return true if the key was handled.
- // We use it to allow return in the signature field
-
- Boolean TInternetAssistantDialog::FilterKey(const EventRecord& event, UInt16 key)
- {
- Boolean result;
-
- if (key == keyReturn && GetDialogPane() == kSignaturePane)
- {
- short start;
- short end;
-
- GetSelectionEndpoints(start, end);
-
- if (start == 0 && end >= GetItemTextSize())
- {
- // If the text is entirely selected,
- // handle return, etc. as shortcuts
- result = TAssistant::FilterKey(event, key);
- }
- else
- {
- result = false;
- }
- }
- else
- {
- result = TAssistant::FilterKey(event, key);
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // Pulse
- // --------------------------------------------------------------------
- // Hook function called at Idle time.
- // We use it to build bitmaps for the map
-
- void TInternetAssistantDialog::Pulse(void)
- {
- // If the location pane is the current pane...
- if (GetDialogPane() == kLocationPane)
- {
- // give some time to the map item.
- // The Map item will use it to build the bitmaps
- // for the different regions
- // (since this operation takes some time, we do
- // it asynchronously)
- fMap.Pulse();
- }
-
- TAssistant::Pulse();
- }
-
-
-
- // --------------------------------------------------------------------
- // ItemHit
- // --------------------------------------------------------------------
- // Hook function called when an item is selected
-
- void TInternetAssistantDialog::ItemHit(UInt16 pane, short item)
- {
-
- if (pane == kLocationPane)
- {
- // If the location pane was clicked, get the location
- // that was clicked on from the fMap object.
- Str255 value;
- GetIndString(value, 300, (fMap.GetValue() - 1) * 4 + 1);
- SetParameter("\p<location>", value);
- }
- else if (pane == kEmailOnePane)
- {
- if (item == kPasswordField)
- {
- // Clicked (or typed) in the password field
- // Check to see if the password has changed.
- // If so, save the value in fPassword
- {
- Str255 newPassword;
- Str255 oldPassword;
-
- GetIndString(oldPassword, 301, 4);
- oldPassword[0] = fPassword[0];
- GetItemText(kPasswordField, newPassword);
-
- if (fPasswordChanged ||
- !EqualString(newPassword, oldPassword, true, true))
- {
- fPasswordChanged = true;
- BlockMoveData(newPassword,
- fPassword, newPassword[0] + 1);
- }
- }
- // If the password was not changed, it was probably
- // clicked-on. Select it.
- if (!fPasswordChanged)
- SelectItemText(kPasswordField);
- }
- else
- {
- // Clicked on some other item...
- // replace the password by bullets
- // The real password is contained in fPassword
- Str255 bulletsString;
-
- GetIndString(bulletsString, 301, 4); // "•••••••••"
- bulletsString[0] = fPassword[0];
- SetParameter("\p<password>", bulletsString);
- SetItemText(kPasswordField, bulletsString);
- {
- SInt32 ignore;
- Delay(8, &ignore);
- }
- fPasswordChanged = false;
- }
- }
-
- // Some other item. Let the superclass handle it
- TAssistant::ItemHit(pane, item);
- }
-
-
-
- // --------------------------------------------------------------------
- // SetDialogPane
- // --------------------------------------------------------------------
- // Change to the indicated dialog pane
- // Overriden here so we get a chance to set default values for
- // panes before they get displayed
-
- void TInternetAssistantDialog::SetDialogPane(SInt16 pane)
- {
- if (pane == kSignaturePane)
- {
- Str255 signature;
- Str255 emailAddress;
- GetParameter("\p<signature>", signature);
- GetParameter("\p<email>", emailAddress);
- // If no signature was specificed, but an e-mail address was...
- if (signature[0] == 0 && emailAddress[0] != 0)
- {
- GetIndString(signature, 301, 1);
- SetParameter("\p<signature>", signature);
- }
- }
- else if (pane == kEmailOnePane)
- {
- // When the password pane is displayed, note that
- // the password hasn't been changed yet.
- fPasswordChanged = false;
- }
- else if (pane == kEmailTwoPane)
- {
- Str255 emailAccount;
- Str255 emailHost;
- GetParameter("\p<email account>", emailAccount);
- GetParameter("\p<SMTP host>", emailHost);
- // If the e-mail account or host are not specified...
- if (emailAccount[0] == 0 || emailHost[0] == 0)
- {
- // ... assign them default values based on the host
- // extracted from the e-mail address
- Str255 emailAddress;
- Str255 host;
- GetParameter("\p<email>", emailAddress);
- GetHostName(emailAddress, host);
- if (emailAccount[0] == 0)
- SetParameter("\p<email account>", host);
- if (emailHost[0] == 0)
- SetParameter("\p<SMTP host>", host);
- }
- }
- else if (pane == kHomePagePane)
- {
- Str255 homePage;
-
- GetParameter("\p<home page>", homePage);
- // If there is no home page suggested already...
- if (homePage[0] == 0)
- {
- // ... propose one
- GetIndString(homePage, 301, 3);
- SetParameter("\p<home page>", homePage);
- }
- }
-
- TAssistant::SetDialogPane(pane);
- }
-
-
-
- // --------------------------------------------------------------------
- // GetDefaultValues
- // --------------------------------------------------------------------
- // Collect default values from Internet Config
- // ??? We could also collect the default location from the Map
- // control panel
-
- void TInternetAssistantDialog::GetDefaultValues(void)
- {
- OSErr err;
-
- err = fInternetConfig.ICBegin(icReadOnlyPerm);
- if (err == noErr)
- {
- Str255 value;
-
- fInternetConfig.ICGetString(kICRealName, value);
- if (value[0] == 0)
- {
- // If no name set, get the one from FileSharing
- StringHandle userName;
- userName = GetString(kUserNameSTR);
- if (userName != NULL)
- {
- BlockMoveData(*userName, value, *userName[0] + 1);
- ReleaseResource((Handle)userName);
- }
- }
- SetParameter("\p<user name>", value);
- fInternetConfig.ICGetString(kICOrganization, value);
- SetParameter("\p<organization>", value);
- fInternetConfig.ICGetText(kICSignature, value);
- SetParameter("\p<signature>", value);
- fInternetConfig.ICGetString(kICEmail, value);
- SetParameter("\p<email>", value);
- fInternetConfig.ICGetString(kICMailAccount, value);
- SetParameter("\p<email account>", value);
- fInternetConfig.ICGetScrambledString(kICMailPassword, fPassword);
- GetIndString(value, 301, 4);
- value[0] = fPassword[0];
- SetParameter("\p<password>", value);
- fInternetConfig.ICGetString(kICSMTPHost, value);
- SetParameter("\p<SMTP host>", value);
- fInternetConfig.ICGetString(kICNNTPHost, value);
- SetParameter("\p<NNTP host>", value);
- fInternetConfig.ICGetString(kICWWWHomePage, value);
- SetParameter("\p<home page>", value);
-
- err = fInternetConfig.ICEnd();
- assert(err == noErr);
- }
- else
- {
- Str255 value;
- value[0] = 0;
-
- SetParameter("\p<user name>", value);
- SetParameter("\p<organization>", value);
- SetParameter("\p<signature>", value);
- SetParameter("\p<email>", value);
- SetParameter("\p<email account>", value);
- SetParameter("\p<password>", value);
- SetParameter("\p<SMTP host>", value);
- SetParameter("\p<home page>", value);
- SetParameter("\p<NNTP host>", value);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // GoAhead
- // --------------------------------------------------------------------
- // Save the information collected during the interview
- // to Internet Config
-
- void TInternetAssistantDialog::GoAhead(void)
- {
- OSErr err = fInternetConfig.ICBegin(icReadWritePerm);
- if (err == noErr)
- {
- Str255 value;
-
- GetParameter("\p<user name>", value);
- fInternetConfig.ICSetString(kICRealName, value);
-
- GetParameter("\p<organization>", value);
- fInternetConfig.ICSetString(kICOrganization, value);
-
- GetParameter("\p<signature>", value);
- fInternetConfig.ICSetText(kICSignature, value);
-
- GetParameter("\p<email>", value);
- fInternetConfig.ICSetString(kICEmail, value);
-
- GetParameter("\p<email account>", value);
- fInternetConfig.ICSetString(kICMailAccount, value);
-
- fInternetConfig.ICSetScrambledString(kICMailPassword, fPassword);
-
- GetParameter("\p<SMTP host>", value);
- fInternetConfig.ICSetString(kICSMTPHost, value);
-
- GetParameter("\p<NNTP host>", value);
- fInternetConfig.ICSetString(kICNNTPHost, value);
-
- GetParameter("\p<home page>", value);
- fInternetConfig.ICSetString(kICWWWHomePage, value);
-
- {
- SInt16 mapValue = fMap.GetValue();
- if (mapValue > 0)
- {
- GetIndString(value, 300, (mapValue - 1) * 4 + 2);
- fInternetConfig.ICSetString(kICArchiePreferred, value);
- GetIndString(value, 300, (mapValue - 1) * 4 + 3);
- fInternetConfig.ICSetString(kICInfoMacPreferred, value);
- GetIndString(value, 300, (mapValue - 1) * 4 + 4);
- fInternetConfig.ICSetString(kICUMichPreferred, value);
- }
- }
-
- err = fInternetConfig.ICEnd();
- assert(err == noErr);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // SkipPane
- // --------------------------------------------------------------------
- // Return true if we want to skip a pane
- // We show the "e-mail error" pane only if the e-mail entered
- // did not contain a host name
-
- Boolean TInternetAssistantDialog::SkipPane(SInt16 pane)
- {
- Boolean result = false;
-
- if (pane == kEmailError)
- {
- // Check to see if we want to skip the mail error pane
- // This pane is displayed only if the e-mail address entered
- // does not contain a host specification (i.e. a @ character)
- Str255 emailAddress;
- Str255 host;
- GetParameter("\p<email>", emailAddress);
- GetHostName(emailAddress, host);
- result = host[0] != 0;
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // NextPane
- // --------------------------------------------------------------------
- // Function called to query the ID of the next pane.
- // Returns 0 if no more panes
- // Override this function if you need to present panes in non-sequential
- // order or if you want to skip a long sub-interview.
- // For the Internet Assistant, we could have made all the panes
- // sequentials. We have the "e-mail error" pane out of sequence
- // to demonstate this technique.
-
- SInt16 TInternetAssistantDialog::NextPane(SInt16 currentPane)
- {
- if (currentPane == kEmailOnePane)
- return kEmailError;
- else if (currentPane == kEmailError)
- return kEmailTwoPane;
- else
- return TAssistant::NextPane(currentPane);
- }
-